package com.blb.controller;

import com.blb.entity.Menu;
import com.blb.entity.Provinces;
import com.blb.entity.User;
import com.blb.service.IMenuService;
import com.blb.service.IProvincesService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

//@SessionAttributes(value = {"aaa"})
@Controller
public class WelcomeController {

    @Resource
    IMenuService menuService;

    @Autowired
    public IProvincesService provincesService;
    @GetMapping("/")
    public String login(Model model ){
        //model.addAttribute("aaa","asd");
        return "login";
    }
    @GetMapping("toregister")
    public String register(Model model){
        List<Provinces> Provinces = provincesService.list();
        model.addAttribute("Provinces",Provinces);
        return "register";
    }

    @GetMapping("{m}/{p}Page")
    public String page(@PathVariable String m, @PathVariable String p){
        return m + "/" + p;
    }

    @GetMapping("/index")
    public String index (Map<String,Object> map){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user==null){
            return "redirect:/";
        }else {
            List<Menu> menus = menuService.getMenuByUserName(user.getUserAccount(), "0");
            System.out.println(menus);
            map.put("menus",menus);
            return "index";
        }
    }

    @GetMapping("{m}/{p}/{page}Page")
    public String page(@PathVariable String m, @PathVariable String p, @PathVariable String page){
        return m + "/" + p + "/" + page;
    }


}
